package com.ruoyi.product.service.impl;

import com.ruoyi.product.domain.Brand;
import com.ruoyi.product.domain.BrandCategory;
import com.ruoyi.product.domain.Category;
import com.ruoyi.product.mapper.BrandCategoryMapper;
import com.ruoyi.product.mapper.BrandMapper;
import com.ruoyi.product.mapper.CategoryMapper;
import com.ruoyi.product.service.IBrandCategoryService;
import com.ruoyi.product.vo.BrandCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 品牌分类关联Service业务层处理
 *
 * @author syl
 * @date 2025-08-11
 */
@Service
public class BrandCategoryServiceImpl implements IBrandCategoryService {
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询品牌分类关联
     *
     * @param brandId 品牌分类关联主键
     * @return 品牌分类关联
     */
    @Override
    public List<BrandCategory> selectBrandCategoryByBrandId(Long brandId) {
        return brandCategoryMapper.selectBrandCategoryByBrandId(brandId);
    }

    /**
     * 查询品牌分类关联列表
     *
     * @param brandCategory 品牌分类关联
     * @return 品牌分类关联
     */
    @Override
    public List<BrandCategory> selectBrandCategoryList(BrandCategory brandCategory) {
        return brandCategoryMapper.selectBrandCategoryList(brandCategory);
    }

    /**
     * 新增品牌分类关联
     *
     * @param brandCategory 品牌分类关联
     * @return 结果
     */
    @Override
    public int insertBrandCategory(BrandCategory brandCategory) {
        List<BrandCategory> brandCategories = brandCategoryMapper.selectBrandCategoryList(new BrandCategory(brandCategory.getCategoryId()));
        if (!brandCategories.isEmpty()) {
            throw new RuntimeException("该分类已关联品牌");
        }
        return brandCategoryMapper.insertBrandCategory(brandCategory);
    }

    /**
     * 修改品牌分类关联
     *
     * @param brandCategory 品牌分类关联
     * @return 结果
     */
    @Override
    public int updateBrandCategory(BrandCategory brandCategory, long oldCategoryId) {
        if (brandCategory.getCategoryId().equals(oldCategoryId)){
            throw new RuntimeException("未修改分类");
        }
        Long brandId = brandCategory.getBrandId();
        Long categoryId = brandCategory.getCategoryId();
        List<BrandCategory> brandCategories = brandCategoryMapper.selectBrandCategoryByBrandId(brandId);

        // 判断该分类是否已经关联了品牌
        for (BrandCategory b : brandCategories) {
            if (b.getCategoryId().equals(categoryId)){
                throw new RuntimeException("请勿重复关联分类");
            }
        }

        return brandCategoryMapper.updateBrandCategory(brandCategory,oldCategoryId);
    }

    /**
     * 批量删除品牌分类关联
     *
     * @param brandIds 需要删除的品牌分类关联主键
     * @return 结果
     */
    @Override
    public int deleteBrandCategoryByBrandIds(Long[] brandIds, Long brandId) {
        return brandCategoryMapper.deleteBrandCategoryByBrandIds(brandIds, brandId);
    }

    /**
     * 删除品牌分类关联信息
     *
     * @param brandId 品牌分类关联主键
     * @return 结果
     */
    @Override
    public int deleteBrandCategoryByBrandId(Long brandId) {
        return brandCategoryMapper.deleteBrandCategoryByBrandId(brandId);
    }

    @Override
    public List<BrandCategoryVo> listBrandCategoryListVo(List<BrandCategory> list) {
        // 获得id
        List<Long> brandIds = list.stream().filter(Objects::nonNull).map(BrandCategory::getBrandId).collect(Collectors.toList());
        List<Long> categoryIds = list.stream().filter(Objects::nonNull).map(BrandCategory::getCategoryId).collect(Collectors.toList());


        // 获得各名称
        // 将数据先封装到临时map中
        HashMap<Long, String> brandsMap = new HashMap<>();
        HashMap<Long, String> categoryMap = new HashMap<>();

        if (!brandIds.isEmpty()) {
            List<Brand> brands = brandMapper.selectBrandByBrandIds(brandIds);
            brands.forEach(item -> {
                brandsMap.put(item.getBrandId(), item.getName());
            });
        }

        if (!categoryIds.isEmpty()) {
            List<Category> categories = categoryMapper.selectCategoryByCatIds(categoryIds);
            categories.forEach(item -> {
                categoryMap.put(item.getCatId(), item.getName());
            });
        }

        // 整合数据
        ArrayList<BrandCategoryVo> brandCategoryVos = new ArrayList<>();
        list.stream().forEach(item -> {
            BrandCategoryVo brandCategoryVo = new BrandCategoryVo();
            brandCategoryVo.setBrandId(item.getBrandId());
            brandCategoryVo.setCategoryId(item.getCategoryId());
            brandCategoryVo.setBrandName(brandsMap.getOrDefault(item.getBrandId(), ""));
            brandCategoryVo.setCategoryName(categoryMap.getOrDefault(item.getCategoryId(), ""));
            brandCategoryVos.add(brandCategoryVo);
        });

        return brandCategoryVos;
    }
}
